package ar.com.ironsoft.javaopenauth.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import ar.com.ironsoft.javaopenauth.exceptions.JavaOpenAuthException;
import ar.com.ironsoft.javaopenauth.oauth.OAuthService;
import ar.com.ironsoft.javaopenauth.oauth.impl.FacebookOAuthServiceV2;
import ar.com.ironsoft.javaopenauth.oauth.impl.FoursquareOAuthServiceV2;
import ar.com.ironsoft.javaopenauth.oauth.impl.HotmailOAuthServiceWRAP;
import ar.com.ironsoft.javaopenauth.utils.Constants;
import ar.com.ironsoft.javaopenauth.utils.ServletActionType;
import ar.com.ironsoft.javaopenauth.utils.ServletUtils;
import ar.com.ironsoft.javaopenauth.utils.ValidatorUtils;

@SuppressWarnings("serial")
public class OAuthServlet extends HttpServlet{

	private Map<String,OAuthService> oauthServices = new HashMap<String,OAuthService>();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
		doPost(request,response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String uri = request.getRequestURI();
		
		String service = ServletUtils.getServiceFromURI(request.getServletPath(), uri);
		service = StringUtils.upperCase(service);
		String action = ServletUtils.getActionFromURI(request.getServletPath(), uri);
		
		ServletUtils.checkIfServiceIsRegisted(service, oauthServices);
		ServletUtils.checkIfActionIsRegisted(action, oauthServices.get(service).getRegisteredActions());
		
		HttpVisibleServlet servlet = buildActionServlet(action, oauthServices.get(service));
		servlet.doPost(request,response);
		
	}
	
	public void init() throws ServletException{
		String services = getServletConfig().getInitParameter("services");
		String filename = getServletConfig().getInitParameter("oauth_keys");
		
		ValidatorUtils.validateNullOrEmpty(services,filename);
		services = StringUtils.upperCase(services);
		if (StringUtils.contains(services, Constants.OAUTH_SERVICE_HOTMAIL)){
			//Instantiate Hotmail services
			oauthServices.put(Constants.OAUTH_SERVICE_HOTMAIL, new HotmailOAuthServiceWRAP(filename));
		}
		if (StringUtils.contains(services, Constants.OAUTH_SERVICE_FACEBOOK)){
			//Instantiate Facebook services
			oauthServices.put(Constants.OAUTH_SERVICE_FACEBOOK, new FacebookOAuthServiceV2(filename));
		}
		if (StringUtils.contains(services, Constants.OAUTH_SERVICE_FOURSQUARE)){
			//Instantiate Facebook services
			oauthServices.put(Constants.OAUTH_SERVICE_FOURSQUARE, new FoursquareOAuthServiceV2(filename));
		}
	}
	
	private HttpVisibleServlet buildActionServlet(String action,OAuthService service){
		ServletActionType act = ServletActionType.buildEnum(action);
		
		switch (act){
			case LOGIN:
				return new OAuthLoginServlet(service);
			case CALLBACK:
				return new OAuthCallbackServlet(service);
			case REFRESH:
				return new OAuthRefreshServlet(service);
			default:
				throw new JavaOpenAuthException("Invalid action");
		}
		
		
	}
}

